package com.peas.platform.module.icims.expenditure.disbursement.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.expenditure.disbursement.adapter.repository.ExpenditureInternetbankRepository;
import com.peas.platform.module.icims.expenditure.disbursement.adapter.repository.po.ExpenditureInternetbankPo;
import com.peas.platform.module.icims.expenditure.disbursement.domain.ExpenditureInternetbankDomain;
import com.peas.platform.module.icims.expenditure.disbursement.dto.ExpenditureInternetbankDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ExpenditureInternetbankDomainImpl extends BaseDomainImpl<ExpenditureInternetbankDto, ExpenditureInternetbankPo> implements ExpenditureInternetbankDomain {

    @Resource
    private ExpenditureInternetbankRepository expenditureInternetbankRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<ExpenditureInternetbankPo> getMapper() {
        return expenditureInternetbankRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureInternetbankPo>> getRepositoryClass() {
        return ExpenditureInternetbankRepository.class;
    }

    @Transactional
    @Override
    public int save(ExpenditureInternetbankDto dto) {
        if (StrUtil.isBlank(dto.getInternetbankCode())) {
            String code = sequenceService.genSequence("expenditure", "ExpenditureInternetbankDto", "EID", 4, 1);
            dto.setInternetbankCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("internetbankCode", dto.getInternetbankCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(ExpenditureInternetbankDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("internetbankCode", dto.getInternetbankCode());
        return result;
    }

}
